<?php
/**
 * Класс таблицы "roles"
 * 
 * @author naspeh
 * @version $Id: Roles.php 672 2008-06-30 12:08:01Z naspeh $
 */
class Core_Table_Roles extends Core_Table_Resource_Abstract
{
    /**
     * Имя
     *
     * @var string
     */
    protected $_name = 'roles';
    /**
     * Связное поле
     *
     * @var string
     */
    protected $_fieldTie = 'role_id';    
    /**
     * Карта связей
     *
     * @var array
     */
    protected $_referenceMap    = array(
        'Resource' => array(
            'columns'           => array('id'),
            'refTableClass'     => 'Core_Table_Resources',
            'refColumns'        => 'role_id',
        )
    );
    /**
     * Возвращает форму родителей
     *
     * @param Zend_Db_Table_Row $roleEdit
     * @return Core_Form
     */
    public static function getFormParents($roleEdit = null)
    {
        $parents = array();
        if (isset($roleEdit)) {
            $tableRolesTies = new Core_Table_RoleTies();
            $rowset = $tableRolesTies->fetchAll(array('role_id = ?' => $roleEdit->id));
            $parents = array();
            foreach ($rowset as $parent) {
                $parents[$parent->parent] = $parent->parent;
            }
        }
        $tableRoles = new Core_Table_Roles();
        $rowset = $tableRoles->fetchAll('is_system');
        $acl = Core_Acl::getRegisrty();
        $resultForm = new Core_Form(array(
            'withHash' => false,
            'legend' => 'Родительские роли:',
        ));
        $resultForm->setDecorators(
            array(
                'FormElements',
                array('HtmlTag', array('tag' => 'div')),
                array('Fieldset', array('class' => 'span-4') ),
            )
        );
        foreach ($rowset as $role) {
            $resourceRole = new Core_Acl_Resource_Role($role);
            $acl->active($resourceRole);
            if ($acl->isAllowed(Core_Auth::get('id'), $resourceRole)) {
                $resultForm->addElement('checkbox', $role->id, array(
                    'label' => $role->name,
                    'checked' => isset($parents[$role->id]) ? true : false,
                    'decorators' => array(
                        'ViewHelper',
                        array('Label', array('placement'=>'APPEND')),
                        array('HtmlTag', array('tag' => 'div'))
                    )
                ));   
            }
        }
        return $resultForm;
    }
}